package com.supercwn.player.activity;

import android.content.res.Configuration;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Toast;

import com.supercwn.player.R;
import com.supercwn.player.bean.VideoListBean;
import com.superplayer.library.Bean.VideoRightControlDateLiatBean;
import com.superplayer.library.Bean.VideoRightControlListBean;
import com.superplayer.library.SuperPlayer;

import java.util.ArrayList;
import java.util.List;

/**
 * 类描述：视频详情页
 *
 * @author Super南仔
 * @time 2016-9-19
 */
public class SuperVideoDetailsActivity extends AppCompatActivity implements View.OnClickListener, SuperPlayer.OnNetChangeListener {


    private String path = "http://192.168.3.214:8112/MP4/bb17a14b42654b9e8d57a48bcff2fe4e/index.mp4?AuthInfo=vdNn0nS2ob5PU%2Fxn88AfmWaMrbljMFru8edlX7Jha4YuK2YXfPlru4UnwE%2BVp%2B%2Frdntl8nd786MN%0ANJ9KVt9ebFCtJHHD%2FEwBhwJ3PNJYql1aiC9laR7L73xrjwNlKJY2RGevZ%2F7j4CtLKCCe4bfSNQ%3D%3D%0A&version=3";
    private String path1 = "http://192.168.3.214:8112/MP4/34a014ca1fa846bbb5d6776919cb47e8/index.mp4?AuthInfo=vdNn0nS2ob5PU%2Fxn88AfmWaMrbljMFru8edlX7Jha4aqP4B%2BQmT2BCbgZQN%2BefNMKEz7vJH38g79%0Ax81XgsPxrfnsw%2FSFGA2i8%2Fp89aICYpPglyyPyokiUWF5%2Bd9ZaUrpHmY%2FGi0KTKitlPD2sqAtOg%3D%3D%0A&version=3";
    private String path2 = "http://192.168.3.214:8112/MP4/05f24d3d325a4a978a5bef748e56327c/index.mp4?AuthInfo=vdNn0nS2ob5PU%2Fxn88AfmWaMrbljMFru8edlX7Jha4YujUQ2T94hXUM1F9U6Di5WceCDj%2F2fHK48%0AUR5ZoI9FjgR2sbQlu%2FTlriMMINyAHEJ1nectUZ3PiPq8E6agLt3xNWug1XUq%2FO%2FBWsXfroumpQ%3D%3D%0A&version=3";
    private SuperPlayer player;
    private boolean isLive;
    private boolean isAdvertising = false;//是否有广告，false没有，true有

    /**
     * 测试地址
     */
    private String url;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_super_video_layout);
        initData();
        initView();
        initPlayer();

    }

    /**
     * 初始化相关的信息
     */
    private void initData() {
        isLive = getIntent().getBooleanExtra("isLive", false);
        url = getIntent().getStringExtra("url");
    }

    /**
     * 初始化视图
     */
    private void initView() {
        findViewById(R.id.tv_replay).setOnClickListener(this);
        findViewById(R.id.tv_play_location).setOnClickListener(this);
        findViewById(R.id.tv_play_switch).setOnClickListener(this);
    }

    /**
     * 初始化播放器
     */
    private void initPlayer() {
        player = (SuperPlayer) findViewById(R.id.view_super_player);
        player.setViewRightControl(setData());
        player.setViewRightControlDate(setRightData());
        if (isLive) {
            player.setLive(true);//设置该地址是直播的地址
        }
        player.setNetChangeListener(true)//设置监听手机网络的变化
                .setOnNetChangeListener(this)//实现网络变化的回调
                .onPrepared(new SuperPlayer.OnPreparedListener() {
                    @Override
                    public void onPrepared() {
                        /**
                         * 监听视频是否已经准备完成开始播放。（可以在这里处理视频封面的显示跟隐藏）
                         */
                        Log.i("TGH", "onPrepared");
                    }
                }).onComplete(new Runnable() {
            @Override
            public void run() {
                /**
                 * 监听视频是否已经播放完成了。（可以在这里处理视频播放完成进行的操作）
                 */
                Log.i("TGH", "onComplete");
                if (isAdvertising) {
                    player.playSwitch(url);
                }
            }
        }).onInfo(new SuperPlayer.OnInfoListener() {
            @Override
            public void onInfo(int what, int extra) {
                /**
                 * 监听视频的相关信息。
                 */
                Log.i("TGH", "onInfo   " + what);
            }
        }).onError(new SuperPlayer.OnErrorListener() {
            @Override
            public void onError(int what, int extra) {
                /**
                 * 监听视频播放失败的回调
                 */
                Log.i("TGH", what + "  onError  " + extra);
            }
        }).setTitle(url)//设置视频的titleName
                .play(url);//开始播放视频
        isAdvertising = true;
        player.setScaleType(SuperPlayer.SCALETYPE_FITXY);

    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.tv_replay) {
            if (player != null) {
                player.play(url);
            }
        } else if (view.getId() == R.id.tv_play_location) {
            if (isLive) {
                Toast.makeText(this, "直播不支持指定播放", Toast.LENGTH_SHORT).show();
                return;
            }
            if (player != null) {
                /**
                 * 这个节点是根据视频的大小来获取的。不同的视频源节点也会不一致（一般用在退出视频播放后保存对应视频的节点从而来达到记录播放）
                 */
                player.play(url, 89528);
            }
        } else if (view.getId() == R.id.tv_play_switch) {
            /**
             * 切换视频播放源（一般是标清，高清的切换ps：由于我没有找到高清，标清的视频源，所以也是换相同的地址）
             */
            if (isLive) {
                player.playSwitch(url);
            } else {
                player.playSwitch(path);
            }
        }
    }

    /**
     * 网络链接监听类
     */
    @Override
    public void onWifi() {
        Toast.makeText(this, "当前网络环境是WIFI", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onMobile() {
        Toast.makeText(this, "当前网络环境是手机网络", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onDisConnect() {
        Toast.makeText(this, "网络链接断开", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onNoAvailable() {
        Toast.makeText(this, "无网络链接", Toast.LENGTH_SHORT).show();
    }


    /**
     * 下面的这几个Activity的生命状态很重要
     */
    @Override
    protected void onPause() {
        super.onPause();
        if (player != null) {
            player.onPause();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (player != null) {
            player.onResume();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (player != null) {
            player.onDestroy();
        }
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if (player != null) {
            player.onConfigurationChanged(newConfig);
        }
    }

    @Override
    public void onBackPressed() {
        if (player != null && player.onBackPressed()) {
            return;
        }
        super.onBackPressed();
    }

    private List<VideoRightControlListBean> dataList = new ArrayList<>();


    private List<VideoRightControlListBean> setData() {
        dataList.clear();
        VideoRightControlListBean bean1 = new VideoRightControlListBean();
        bean1.setName("欢乐喜剧人");
        bean1.setVideoUrl("http://baobab.wandoujia.com/api/v1/playUrl?vid=9502&editionType=normal");
        dataList.add(bean1);
        VideoRightControlListBean bean2 = new VideoRightControlListBean();
        bean2.setName("神医喜来乐");
        bean2.setVideoUrl("http://baobab.wandoujia.com/api/v1/playUrl?vid=9508&editionType=normal");
        dataList.add(bean2);
        VideoRightControlListBean bean3 = new VideoRightControlListBean();
        bean3.setName("新闻资讯");
        bean3.setVideoUrl("http://baobab.wandoujia.com/api/v1/playUrl?vid=8438&editionType=normal");
        dataList.add(bean3);
        VideoRightControlListBean bean4 = new VideoRightControlListBean();
        bean4.setName("山外有山");
        bean4.setVideoUrl("http://baobab.wandoujia.com/api/v1/playUrl?vid=8340&editionType=normal");
        dataList.add(bean4);
        VideoRightControlListBean bean5 = new VideoRightControlListBean();
        bean5.setName("近水楼台先得月");
        bean5.setVideoUrl("http://baobab.wandoujia.com/api/v1/playUrl?vid=9392&editionType=normal");
        dataList.add(bean5);
        VideoRightControlListBean bean6 = new VideoRightControlListBean();
        bean6.setName("天下第一关");
        bean6.setVideoUrl("http://baobab.wandoujia.com/api/v1/playUrl?vid=7524&editionType=normal");
        dataList.add(bean6);
        VideoRightControlListBean bean7 = new VideoRightControlListBean();
        bean7.setName("大水冲了龙王庙");
        bean7.setVideoUrl("http://baobab.wandoujia.com/api/v1/playUrl?vid=9444&editionType=normal");
        dataList.add(bean7);
        VideoRightControlListBean bean8 = new VideoRightControlListBean();
        bean8.setName("蜘蛛侠");
        bean8.setVideoUrl("http://baobab.wandoujia.com/api/v1/playUrl?vid=9442&editionType=normal");
        dataList.add(bean8);
        VideoRightControlListBean bean9 = new VideoRightControlListBean();
        bean9.setName("侠客岛");
        bean9.setVideoUrl("http://baobab.wandoujia.com/api/v1/playUrl?vid=8530&editionType=normal");
        dataList.add(bean9);
        VideoRightControlListBean bean10 = new VideoRightControlListBean();
        bean10.setName("哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈");
        bean10.setVideoUrl("http://baobab.wandoujia.com/api/v1/playUrl?vid=9418&editionType=normal");
        dataList.add(bean10);
        VideoRightControlListBean bean11 = new VideoRightControlListBean();
        bean11.setName("开心魔法");
        bean11.setVideoUrl("http://192.168.3.214:8112/MP4/a295ef92710b4d7a93861f9a963798b7/index.mp4?AuthInfo=F0JT0y6aXCnoyXl95HEaxk9xBlTfqf5tgJivQlQ%2F9p%2FS4X76lecM3Q6Hwyw8L%2F7iJMWVoPhtgyST%0ABAPxJ3pvqAykzStG4t17jI4%2F1Txkh287PvLQ7dEUKmA64GVzFsLYlsrhMEs4SxetJ0Ck5lOx1A%3D%3D%0A&version=3");
        dataList.add(bean11);

        return dataList;
    }

    private List<VideoRightControlDateLiatBean> dataListRight = new ArrayList<>();

    private List<VideoRightControlDateLiatBean> setRightData() {
        dataListRight.clear();
        VideoRightControlDateLiatBean bean1 = new VideoRightControlDateLiatBean();
        bean1.setWeek("周一");
        bean1.setDate("26");
        dataListRight.add(bean1);
        VideoRightControlDateLiatBean bean2 = new VideoRightControlDateLiatBean();
        bean2.setWeek("周二");
        bean2.setDate("27");
        dataListRight.add(bean2);
        VideoRightControlDateLiatBean bean3 = new VideoRightControlDateLiatBean();
        bean3.setWeek("周三");
        bean3.setDate("28");
        dataListRight.add(bean3);
        VideoRightControlDateLiatBean bean4 = new VideoRightControlDateLiatBean();
        bean4.setWeek("周四");
        bean4.setDate("29");
        dataListRight.add(bean4);
        VideoRightControlDateLiatBean bean5 = new VideoRightControlDateLiatBean();
        bean5.setWeek("周五");
        bean5.setDate("30");
        dataListRight.add(bean5);
        VideoRightControlDateLiatBean bean6 = new VideoRightControlDateLiatBean();
        bean6.setWeek("周六");
        bean6.setDate("1");
        dataListRight.add(bean6);
        VideoRightControlDateLiatBean bean7 = new VideoRightControlDateLiatBean();
        bean7.setWeek("周日");
        bean7.setDate("2");
        dataListRight.add(bean7);
        VideoRightControlDateLiatBean bean8 = new VideoRightControlDateLiatBean();
        bean8.setWeek("周一");
        bean8.setDate("3");
        dataListRight.add(bean8);
        VideoRightControlDateLiatBean bean9 = new VideoRightControlDateLiatBean();
        bean9.setWeek("周二");
        bean9.setDate("4");
        dataListRight.add(bean9);
        VideoRightControlDateLiatBean bean10 = new VideoRightControlDateLiatBean();
        bean10.setWeek("周三");
        bean10.setDate("5");
        dataListRight.add(bean10);
        return dataListRight;
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (KeyEvent.KEYCODE_BACK == keyCode) {
            this.finish();
        }
        return super.onKeyDown(keyCode, event);
    }
}
